<?php

namespace App\Admin\Controllers;

use App\Models\ProductAttrKey;
use Encore\Admin\Controllers\AdminController;
use App\Admin\Actions\Post\DeleteAttrKey;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;


class ProductAttrKeyController extends AdminController
{
    protected $title = '商品属性';

    protected function grid()
    {
        $grid = new Grid(new ProductAttrKey());

        $grid->column('id', __('Id号'));
        $grid->column('attr_name', __('属性名称'))->modal('属性值', function ($model) {

            $vals = $model->productAttrVal()->get()->map(function ($val) {
                return $val->only(['id', 'attr_value', 'created_at', 'updated_at']);
            });

            return new Table(['ID', '属性值', '创建时间', '更新时间'], $vals->toArray());
        });
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->add(new DeleteAttrKey);
        });

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(ProductAttrKey::findOrFail($id));

        $show->field('id', __('Id号'));
        $show->field('attr_name', __('属性名称'));

        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));
        $show->productAttrVal('属性值列表', function ($vals) {
            $vals->resource('/admin/product_attr_val');
            $vals->id('ID号');
            $vals->attr_value('属性值');
            $vals->created_at('创建时间');
            $vals->updated_at('更新时间');

            $vals->disableCreateButton();

//            $comments->filter(function ($filter) {
//                $filter->like('content');
//            });
        });
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });

        return $show;
    }

    protected function form()
    {
        $form = new Form(new ProductAttrKey());

        $form->text('attr_name', __('属性名称'));
        $form->hasMany('productAttrVal', '', function (Form\NestedForm $form) {
            $form->text('attr_value', '属性值');
        });
        $form->footer(function ($footer) {
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
        });

        return $form;
    }
}